package ${groupId}.${module}.controller.web;

import ${groupId}.server.dto.${Domain}Dto;
import ${groupId}.server.dto.PageDto;
import ${groupId}.server.dto.ResponseDto;
import ${groupId}.server.service.${Domain}Service;
import ${groupId}.server.utils.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* @author: duck
* @date: 2020-09-17 18:44
* @desc:${tableNameCn}控制类
**/
@Slf4j
@RestController
@RequestMapping("/web/${domain}")
public class ${Domain}Controller {
    public static final String BUSINESS_NAME="${tableNameCn}";

    @Resource
    private ${Domain}Service ${domain}Service;

    /**
    * 获取${tableNameCn}列表
    * @param page
    * @param size
    * @return
    */
    @GetMapping("/list")
    public ResponseDto list(@RequestParam(value = "page",defaultValue = "1")Integer page,@RequestParam(value = "size",defaultValue = "10") Integer size){
        ResponseDto responseDto = new ResponseDto();
        PageDto pageDto=new PageDto();
        pageDto.setPage(page);
        pageDto.setSize(size);
        ${domain}Service.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
    * 保存${tableNameCn}信息
    * @param ${domain}Dto
    * @return
    */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody ${Domain}Dto ${domain}Dto) {
        //保存校验
<#list fieldList as field>
    <#if field.name!="id"&&field.nameHump!="createdAt"&&field.nameHump!="updatedAt"&&field.nameHump!="sort">
        <#if !field.nullAble>
        ValidatorUtil.require(${domain}Dto.get${field.nameBigHump}(),"${field.nameCn}");
        </#if>
        <#if (field.length>0)>
        ValidatorUtil.length(${domain}Dto.get${field.nameBigHump}(),"${field.nameCn}",1,${field.length?c});
        </#if>
    </#if>
</#list>
        ResponseDto responseDto = new ResponseDto();
        ${domain}Service.save(${domain}Dto);
        responseDto.setContent(${domain}Dto);
        return responseDto;
    }

    /**
    * 根据主键删除${tableNameCn}信息
    * @param id
    * @return
    */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
<#list fieldList as fieId>
        <#if fieId.nameHump=='id'&&fieId.javaType=='String'>
        ${domain}Service.delete(id);
        </#if>
        <#if fieId.nameHump=='id'&&fieId.javaType=='Integer'>
        ${domain}Service.delete(Integer.parseInt(id));
        </#if>
        <#if fieId.nameHump=='id'&&fieId.javaType=='Long'>
        ${domain}Service.delete(Long.parseLong(id));
        </#if>
</#list>
        return responseDto;
    }
}